﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Group_Buy
{
    public partial class Form : FormPage
    {
        BLL.GroupBuy bll = new BLL.GroupBuy();
        Model.GroupBuy gbmodel = new Model.GroupBuy();
        protected EnumHelper.UserType userType;
        protected bool isAdmin;
        protected void Page_Load(object sender, EventArgs e)
        {
            userType = Security.GetCurrentUserType();
            isAdmin = userType == EnumHelper.UserType.Administrator;
        }

        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { gbmodel = bll.GetModel(Master.id); }

        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            Master.HideSubmit();
        }

        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            if (Master.formState == EnumHelper.FormState.edit)
            {
                tb_Name.Text = gbmodel.gbname;
                tb_Name.Enabled = false;
                tb_price.Text = StringPlus.showCoin(gbmodel.gbprice);
                tb_endtime.Text = gbmodel.gbendtime.ToString("yyyy-MM-dd");
                DDL_Supplier.SelectedValue = DataTypePlus.requestString(gbmodel.gbsupplierid);
                tb_Description.Text = gbmodel.gbcontent;
                tb_Description.Enabled = false;
                BBPA.BLL.Group_Good gg_bll=new BLL.Group_Good();
                DataTable dt = gg_bll.GetGroupBuy_GoodList(" gg_gbid=" + Master.id).Tables[0];
                this.Repeater1.DataSource = dt;
                this.Repeater1.DataBind();
                this.Repeater1.Visible = true;
            }
            
        }

         /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            
          
        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));
            decimal price = DataTypePlus.requestDecimal(tb_price.Text);
            if (price < 1)
            {
                return;
            }
            BLL.Message bll_msg = new BLL.Message();
            DateTime endtime = Convert.ToDateTime(DataTypePlus.requestString(tb_endtime.Text));
            int supplierid = DataTypePlus.requestInt(DDL_Supplier.SelectedValue);
            string content = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Description.Text));
            if (Master.formState == EnumHelper.FormState.add)
            {
                gbmodel.gbname = Name;
                gbmodel.gbsupplierid = supplierid;
                gbmodel.gbprice = price;
                gbmodel.gbcontent = content;
                gbmodel.gbendtime = endtime;
                gbmodel.gbstatus = 1;
                int id = bll.Add(gbmodel);
                if (id > 0)
                {
                    BBPA.BLL.Group_Good gg_bll = new BLL.Group_Good();
                    string[] array = goodarray.Value.Split(new string[1] { "@#@" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < array.Length; i++)
                    {
                        string[] goods = array[i].Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        BBPA.Model.Group_Good ggmodel = new Model.Group_Good();
                        ggmodel.gg_gbid = id;
                        ggmodel.gg_goodid = Convert.ToInt32(goods[0]);
                        ggmodel.gggoodprice = Convert.ToDecimal(goods[1]);
                        gg_bll.Add(ggmodel);
                    }
                    int userID = Security.GetCurrentUserID();
                    bll_msg.NewMessage2UserType(Master.id, userID, 2, gbmodel.gbcontent, true, true);
                }

            }
            else
            {
                gbmodel.gbprice = price;
                gbmodel.gbendtime = endtime;
                bll.Update(gbmodel);
            }
            BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.GroupBuy, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
            Master.SubmitOK();
        }

        protected void btnfq_Click(object sender, EventArgs e)
        {
            Model.Orders model = new Model.Orders();
            BLL.Orders bll_order = new BLL.Orders();
            BLL.Store bllStore = new BLL.Store();
            BLL.V_Orders bllList = new BLL.V_Orders();
            BLL.Orders_State_Log bllOrders_State_Log = new BLL.Orders_State_Log();
            List<BBPA.Model.V_Orders> modellist = bllList.GetModelList(" gbid=" + Master.id);
            foreach (BBPA.Model.V_Orders v_model in modellist)
            {
                if (v_model.ExpectPay > 0)
                {
                    model = bll_order.GetModel(v_model.id);
                    Model.Store modelStore = bllStore.GetModel(model.StoreID);
                    //1.支付货款
                    CallBak<int> callBak_BuySuccess = BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, model.StoreID, EnumHelper.AccountChangeReason.Cancel_Orders, v_model.id, 0);//扣孕婴店
                    if (!callBak_BuySuccess.IsSuccess) { Master.ScriptManager_Alert_float(callBak_BuySuccess.ErrMsg, false); return; }

                    //2.扣运费
                    CallBak<int> callBak_PayExpressMoney = BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, modelStore.id, EnumHelper.AccountChangeReason.Pay_ExpressMoney, v_model.id, 0);
                    if (!callBak_PayExpressMoney.IsSuccess) { Master.ScriptManager_Alert_float(callBak_PayExpressMoney.ErrMsg, false); return; }

                    model.ExpressID = 0;
                    model.ExpressPrice = 0;
                    model.ExpressNumber = "";
                    model.State = (int)EnumHelper.OrdersState.NoGoods;
                    model.DateandtimeSend = DateTime.Now;
                    model.IsBuyMoneyOK = true;
                    bll_order.Update(model);

                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders, Master.id, EnumHelper.OperateType.Update, "");
                    //订单状态日志
                    Model.Orders_State_Log modelOrders_State_Log = new Model.Orders_State_Log();
                    modelOrders_State_Log.OrdersID = Master.id;
                    modelOrders_State_Log.State = model.State;
                    int Orders_State_LogID_new = bllOrders_State_Log.Add(modelOrders_State_Log);
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_State_Log, Orders_State_LogID_new, EnumHelper.OperateType.Add, "");
                    //扣除运费后，孕婴店金额发生变化，所以要重新获取modelStore
                    modelStore = bllStore.GetModel(model.StoreID);
                    //通知孕婴店
                    BLL.Message bllMessage = new BLL.Message();
                    bllMessage.NewMessage(0, Security.GetCurrentUserID(), modelStore.UserID,
                        "您参与的团购已失败，共计金额" + StringPlus.showMoney(model.ExpectPay) + "元，运费100元，已返回您的账户，您帐户剩余金额为" + modelStore.Account + "元", true, true);
                }
                else
                {
                    bll_order.Delete(v_model.id);
                    Maticsoft.DBUtility.DbHelperSQL.ExecuteSql("delete from Orders_Goods where  OrdersID=" + v_model.id);
                }

            }
            gbmodel.gbstatus = 4;
            bll.Update(gbmodel);

            Master.SubmitOK("放弃成功");

            
        }
    }
}